const Router = require('koa-router');
const mongoose = require('mongoose');
const path = require('path');
const { uploadFile } = require('../model/file');

let router = new Router();

router.post('/', async (ctx,next) => {
  const TempPic = mongoose.model('TempPic');
  const serverPath = path.join(__dirname,'../public/uploads/')
  const result = await uploadFile(ctx, {
    fileType: 'album',
    path: serverPath
  })
  const url = "http://localhost:3000/uploads/" + result.imgPath;
  const imgPath = path.join(serverPath,result.imgPath);
  const newPic = new TempPic({
    path: imgPath,
    url: url,
    createAt: new Date(),
  });
  const saveRes = await newPic.save();
  console.log(saveRes);
  console.log(31);
  ctx.status = 200;
  ctx.body = {
    id: saveRes._id,
    imgUrl: url,
  }
})

module.exports = router;